<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSync(scope.row)">手动同步</el-button>
      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(6)): scope.row.count }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
      <template #footerBtn="data">
        已选行数:{{ selectionLength}}
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      selection: null,
      selectionLength:0,
      materialList: [],
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    async handleSync(row) {
      try {
        if (row.orderType === '调拨单') {
          await this.syncListAllocationHistory(row.billTaskNo);
        } else if (row.orderType === '销售单') {
          await this.syncListSaleHistory(row.billTaskNo);
        } else {
          this.$message.warning('单据类型未知');
        }
      } catch (error) {
        this.$message.error(`同步失败: ${error.message}`);
      }
    },
    //调拨单同步金蝶
    async syncListAllocationHistory(allocationId) {
      try {
        await this.$confirm('是否同步此调拨单到金蝶', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        });
        this.$message({ type: 'success', message: '正在同步' });
        this.loading = true;
        let res = await this.callApi('sync_list_allocation_history', { allocationId: allocationId});
        if (!res.res) return;
        this.$message.success('同步成功');
      } catch (error) {
        if (error === 'cancel') {
          this.$message.warning('取消同步');
        } else {
          this.$message.error('同步失败');
        }
      } finally {
        this.loading = false;
      }
    },
    //销售单同步金蝶
    async syncListSaleHistory(tradeNo) {
      try {
        await this.$confirm('是否同步此销售单到金蝶', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'success'
        });
        this.$message({ type: 'success', message: '正在同步' });
        this.loading = true;
        let res = await this.callApi('sync_list_sale_history', { tradeNo: tradeNo});
        if (!res.res) return;
        this.$message.success('同步成功');
      } catch (error) {
        if (error === 'cancel') {
          this.$message.warning('取消同步');
        } else {
          this.$message.error('同步失败');
        }
      } finally {
        this.loading = false;
      }
    },
    async getMaterialList(val) {
      if (!val) {
        this.materialList = []
        return
      }
      this.materialLoading = true
      try {
        const res = await this.callApi('get_sync_list_history', { key: val, pageSize: 20 })
        if (!res.res) return
        this.materialList = res.data
      } finally {
        this.materialLoading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(billNo = '', stockNo = '') {
      let url = '/produce/prdInDetail'
      if (billNo && stockNo) {
        url += `?billNo=${ billNo }&stockNo=${stockNo}`
      }
      this.$router.push(url)
    },
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length ;
    },
  }
}
</script>

<style scoped>

</style>
